PASO 13: Cuando la pelota golpea la cabeza del jugador, ¡queremos que rebote! Una velocidad y negativa hace que la pelota caiga, ¡así que una velocidad positiva la hará rebotar! En cada rebote, negemos y aumentemos ligeramente la velocidad de la pelota.
Ir a y arrastre Set y Speed a la última línea de la función head_ball() .
Cambia el sprite a hit_sprite . ¡En esta colisión, el hit_sprite es la pelota!
Reemplace el 5 entre paréntesis con la ecuación (- my_var + 1 ) . ¡Asegúrate de que my_var sea negativo!
To navigate the page using the TAB key, first press ESC to exit the code editor.
def set_stage():
""" Sets up the stage for the game """
stage.set_background("soccerfield")
stage.disable_floor()
def add_player():
""" Adds a player to the stage for the user to control """
player = codesters.Sprite("player1")
player.go_to(0, -180)
player.set_size(0.75)
return player
def add_ball():
""" Adds a ball to the stage and sets its attributes """
ball = codesters.Sprite("soccerball")
ball.set_y_speed(-8)
def head_ball(sprite, hit_sprite):
""" Detects collisions between the player and ball """
my_var = hit_sprite.get_y_speed()
def main():
""" Sets up the program and calls other functions """
set_stage()
player = add_player()
add_ball()
player.event_collision(head_ball)
main()
t = codesters.Teacher()
defs = t.find_block("def")
set_y_speeds = t.find_function("set_y_speed")
def get_above_def(line_number, def_list):
""" Returns the name of the nearest function and the distance above the given line number """
function_name = ""
distance = line_number - def_list[0][0]
for func in def_list:
if line_number - func[0] <= distance and line_number - func[0] > 0:
function_name = func[1]
distance = line_number - func[0]
return function_name, distance
try:
tval1 = set_y_speeds[1][1].replace(' ','').lower()
tval1_line_num = set_y_speeds[1][0]
tval1_indent = t.get_indent_at_line(tval1_line_num)
except:
tval1 = "DNE"
tval1_line_num = "DNE"
tval1_indent = "DNE"
try:
tval1_def, distance = get_above_def(tval1_line_num, defs)
except:
tval1_def = "DNE"
distance ="DNE"
t1 = TestObjective()
t1.add_success("hit_sprite" in tval1 and "-my_var+1" in tval1, "Great job!")
t1.add_failure(tval1 == "DNE", "Did you add Set y Speed into the head_ball() function?")
t1.add_failure(tval1 != "DNE" and "hit_sprite" not in tval1, "Did you change sprite to hit_sprite in front of .set_y_speed()?")
t1.add_failure(tval1 != "DNE" and "-my_var+1" not in tval1, "Did you add the correct equation within the parentheses of .set_y_speed()?")
t2 = TestObjective()
t2.add_success("head_ball" in tval1_def and tval1_indent == 4, "Great job!")
t2.add_failure("head_ball" not in tval1_def, "Did you place Set y Speed within the head_ball() function?")
t2.add_failure(tval1_indent < 4 or tval1_indent > 4, "Make sure Set y Speed is indented within head_ball()!")
tester = TestManager()
tester.add_test_list([t1, t2])
tester.run_tests()
tester.display_first_feedback()
Are you already running a Codesters project in another tab or window?
Micro:bit can only connect to one web page at a time.
Try stopping other Codesters projects or closing
other tabs or windows that may be using your Micro:bit.
If that doesn't fix the problem try disconnecting your Micro:bit,
reloading this page, and reconnecting your Micro:bit.